# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
import time
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class UserOauthAppDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()


    def queryUserOauthAppById(self, id):
        """
        根据ID查询信息    查询    zzh_user_oauth
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_user_oaurh_app WHERE id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql,[id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthAppJson = Table2JsonByYM.user_oauth_app_list_to_json(result)
            return  userOauthAppJson, total_count
        else:
            cur.close()
            self.conn.close()
            return None, 0

    def queryUserOauthAppByCondition(self, **kwargs):
        """
        条件查询  zzh_user_oauth
        :param :
        :return:
        此处使用userId为条件查询
        """
        appId = kwargs['appId']
        page_index = kwargs['pageIndex']
        PAGE_SIZE = kwargs['pageSize']
        sql = "SELECT * FROM zzh_user_oaurh_app WHERE app_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql,[appId])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            if (page_index * PAGE_SIZE) < total_count:
                start = (page_index - 1) * PAGE_SIZE
                end = page_index * PAGE_SIZE
                userOauthPart = result[start: end]
                userOauthAppJson = Table2JsonByYM.user_oauth_app_list_to_json(userOauthPart)
                return total_count, userOauthAppJson
            else:
                start = (page_index - 1) * PAGE_SIZE
                userOauthPart = result[start: ]
                userOauthAppJson = Table2JsonByYM.user_oauth_app_list_to_json(userOauthPart)
                return total_count, userOauthAppJson

        else:
            cur.close()
            self.conn.close()
            return 0, None


    def addUserOauthApp(self,Info):
        """

        :param userRechargeInfo:
        :return:
        """
        appTitle = Info['appTitle']  # 必填，请传递user_id
        appImgUrl = Info['appImgUrl']  #
        appId = Info['appId']  #
        appKey = Info['appKey']  #
        remark = Info['remark']  #
        appSort = Info['appSort']
        appIsLock = Info['appIsLock']  #
        apiPath = Info['apiPath']
        cur = self.conn.cursor()
        sql = 'INSERT INTO zzh_user_oaurh_app (app_title, app_img_url, app_id, app_key, remark, app_sort,app_is_lock,api_path) VALUES ( %s,%s,%s, %s ,%s, %s, %s, %s)'
        rs=cur.execute(sql,[appTitle,appImgUrl,appId,appKey,remark,appSort,appIsLock,apiPath])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None

    def selectOauthApp(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_user_oaurh_app WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def updateUserOauthApp(self,Info):
        id = Info['id']
        appTitle = Info['appTitle']  # 必填，请传递user_id
        appImgUrl = Info['appImgUrl']  #
        appId = Info['appId']  #
        appKey = Info['appKey']  #
        remark = Info['remark']  #
        appSort = Info['appSort']
        appIsLock = Info['appIsLock']  #
        apiPath = Info['apiPath']
        cur = self.conn.cursor()
        sql = 'UPDATE zzh_user_oaurh_app SET app_title=%s, app_img_url=%s, app_id=%s, app_key=%s, remark=%s,app_sort=%s,app_is_lock=%s,api_path=%s WHERE id=%s'
        rs=cur.execute(sql,[appTitle,appImgUrl,appId,appKey,remark,appSort,appIsLock,apiPath,id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None




    def deleteUserOauthApp(self, ids):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组 []
        :return:
        """
        sql = "delete from zzh_user_oaurh_app WHERE id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs
